package com.example.x6.configurationmaintenance.Service;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import com.example.x6.configurationmaintenance.Notification.NotificationUtil;
import com.example.x6.configurationmaintenance.R;
import com.example.x6.configurationmaintenance.Utils.MD5;
import java.io.File;

public class DownloadService extends IntentService {
    public static final String fileurlKey = "fileurlKey";
    private static boolean isDownload = false;
    public static final String md5Key = "md5Key";
    private final String TAG = getClass().getSimpleName();
    private String fileName = "configurationMaintenance.apk";
    private String filePath = "/sdcard/ConfigurationMaintenance";
    private Handler handler = new Handler();
    private NotificationUtil notificationUtil;

    public DownloadService() {
        super("DownloadService");
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x00bb, code lost:
        r4 = false;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:36:0x0115, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:37:0x0116, code lost:
        r3 = r0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:38:0x0118, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:39:0x0119, code lost:
        r3 = r0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:47:0x0156, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:48:0x0157, code lost:
        r3 = r0;
        r4 = false;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:49:0x015c, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:50:0x015d, code lost:
        r3 = r0;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:36:0x0115 A[Catch:{ MalformedURLException -> 0x0118, IOException -> 0x0115 }, ExcHandler: IOException (r0v4 'e' java.io.IOException A[CUSTOM_DECLARE, Catch:{ MalformedURLException -> 0x0118, IOException -> 0x0115 }]), Splitter:B:31:0x00fd] */
    /* JADX WARNING: Removed duplicated region for block: B:49:0x015c A[ExcHandler: IOException (r0v1 'e' java.io.IOException A[CUSTOM_DECLARE]), Splitter:B:15:0x0087] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void onHandleIntent(@android.support.annotation.Nullable android.content.Intent r24) {
        /*
        // Method dump skipped, instructions count: 425
        */
        throw new UnsupportedOperationException("Method not decompiled: com.example.x6.configurationmaintenance.Service.DownloadService.onHandleIntent(android.content.Intent):void");
    }

    public static synchronized boolean isDownload() {
        boolean z;
        synchronized (DownloadService.class) {
            z = isDownload;
        }
        return z;
    }

    private synchronized void setIsDownload(boolean isDownload1) {
        isDownload = isDownload1;
    }

    private boolean isNewApkExisted(String apkPath, String md5) {
        if (new File(apkPath).exists() && new MD5().getFileMD5(apkPath).equalsIgnoreCase(md5)) {
            return true;
        }
        return false;
    }

    private boolean installApk(String apkPath, String md5) {
        if (!isNewApkExisted(apkPath, md5)) {
            toast(getResources().getString(R.string.file_not_existed_or_invalid));
            return false;
        }
        Intent intent = new Intent("android.intent.action.VIEW");
        File file = new File(apkPath);
        if (Build.VERSION.SDK_INT >= 24) {
            Uri apkUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file);
            intent.addFlags(1);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setFlags(268435456);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        startActivity(intent);
        return true;
    }

    private void setProgress(final int progress) {
        this.handler.post(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Service.DownloadService.AnonymousClass1 */

            public void run() {
                DownloadService.this.notificationUtil.updateProgress(100, progress);
            }
        });
    }

    private void showNotificationUtil() {
        this.handler.post(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Service.DownloadService.AnonymousClass2 */

            public void run() {
                DownloadService.this.notificationUtil.showNotification(100, null);
            }
        });
    }

    private void dismissNotificationUtil() {
        this.handler.post(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Service.DownloadService.AnonymousClass3 */

            public void run() {
                DownloadService.this.notificationUtil.cancel(100);
            }
        });
    }

    private void showResultToast(boolean isSucessufl) {
        final String message;
        if (isSucessufl) {
            message = getResources().getString(R.string.download_success);
        } else {
            message = getResources().getString(R.string.download_failed);
        }
        this.handler.post(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Service.DownloadService.AnonymousClass4 */

            public void run() {
                Toast.makeText(DownloadService.this, message, 0).show();
            }
        });
    }

    private void toast(final String string) {
        if (string != null) {
            this.handler.post(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Service.DownloadService.AnonymousClass5 */

                public void run() {
                    Toast.makeText(DownloadService.this, string, 0).show();
                }
            });
        }
    }
}
